package com.practice.线程池;
import java.util.concurrent.*;
/**
 * Created by IntelliJ IDEA2021.3
 * @Author: Tenghw
 * @Date: 2022/11/05  18:21
 * @Description:
 */
@SuppressWarnings("all")
public class Demo3 {
    private  static ExecutorService pool;
    public static void main1(String[] args) throws Exception{
        try {
            /**
             * 创建一个线程池对象
             * 参数信息：
             * int corePoolSize     核心线程大小
             * int maximumPoolSize  线程池最大容量大小
             * long keepAliveTime   线程空闲时，线程存活的时间
             * TimeUnit unit        时间单位
             * BlockingQueue<Runnable> workQueue 任务队列(一个阻塞队列)
             * Factory 线程工厂
             * Policy 拒接策略
             */
            //无界阻塞队列LinkedBlockingQueue
            //ExecutorService pool = new ThreadPoolExecutor(2,4,2000L, TimeUnit.MILLISECONDS,
            //        new LinkedBlockingQueue<Runnable>(3), Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
            ExecutorService pool = new ThreadPoolExecutor(2,4,2000L, TimeUnit.MILLISECONDS,
                    new SynchronousQueue<Runnable>(), Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
            //直接提交队列
            // pool = new ThreadPoolExecutor(4, 10, 2000L, TimeUnit.MILLISECONDS,
            //        new SynchronousQueue<Runnable>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

            //任务
            Runnable runnable1 = () -> {
                System.out.println("当前线程>>> "+Thread.currentThread().getName());
                System.out.println("~~~~~end~~~~~");
            };
            //执行任务
            for(int i=0;i<9;i++){
                pool.execute(runnable1);
            }
            //关闭线程池
            pool.shutdown();
        }catch (Exception e){e.printStackTrace();}

    }

    public static void main(String[] args) {
        try {
            /**
             * 创建一个线程池对象
             * 参数信息：
             * int corePoolSize     核心线程大小
             * int maximumPoolSize  线程池最大容量大小
             * long keepAliveTime   线程空闲时，线程存活的时间
             * TimeUnit unit        时间单位
             * BlockingQueue<Runnable> workQueue 任务队列(一个阻塞队列)
             * Factory 线程工厂
             * Policy 拒接策略
             */
            //无界阻塞队列LinkedBlockingQueue（但设置了容量=3，该无界队列变为有界队列了）
            ExecutorService pool = new ThreadPoolExecutor(2,4,2000L, TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue<Runnable>(3), Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());

            //任务
            Runnable runnable1 = () -> {
                System.out.println("当前线程>>> "+Thread.currentThread().getName());
                System.out.println("~~~~~end~~~~~");
            };

            //执行任务-测试正常情况
            //for(int i=0;i<7;i++){
            //    pool.execute(runnable1);
            //}
            //执行任务-测试拒绝策略
            for(int i=0;i<8;i++){
                pool.execute(runnable1);
            }
            //Thread.sleep(5000);
            //Thread.currentThread().sleep(1000); 和上一行代码等价
            //关闭线程池
            pool.shutdown();
        }catch (Exception e){e.printStackTrace();}

    }
}
